#include "gps_waypoint.h"
#include <math.h>
#include "gps_leg.h"

#define WAYPOINT_OFFSET_X -8
#define WAYPOINT_OFFSET_Y -43

extern QColor base_colors[];

GPS_waypoint::GPS_waypoint() :
    QGeoCoordinate()
{
    static int current_color = 0;
    static int waypoint_ids = 0;
    _id = waypoint_ids++;
    _name = "";
    _description = "";
    _color = base_colors[ current_color++ ];
    if ( current_color > MAX_BASE_COLOR )
        current_color = 0;
}

/*GPS_waypoint::GPS_waypoint(const GPS_waypoint& other) :
    QGeoCoordinate( other )
{
    _id = other._id;
    _name = other._name;
    _description = other._description;
    _color = other._color;
}*/

int GPS_waypoint::id() const
{
    return _id;
}

QString GPS_waypoint::name() const
{
    return _name;
}

void GPS_waypoint::setName( const QString& n)
{
    if ( n != _name )
    {
        Modify();
        _name = n;
    }
}

QString GPS_waypoint::description() const
{
    return _description;
}

void GPS_waypoint::setDescription( const QString& d)
{
    if ( d != _description )
    {
        Modify();
        _description = d;
    }
}


QColor GPS_waypoint::color() const
{
    return _color;
}

QString GPS_waypoint::LLtoString() const
{
    return "Lat: " + LatToString() + " Lon: " + LonToString();
}

QString GPS_waypoint::LatToString() const
{
    double lat = latitude();
    return QString("%1%2")
                       .arg( fabs(lat), 0, 'f', 5)
                       .arg( lat > 0.0 ? "N" : "S");
}

QString GPS_waypoint::LonToString() const
{
    double lon = longitude();
    return QString("%1%2")
                       .arg( fabs(lon), 0, 'f', 5)
                       .arg( lon > 0.0 ? "E" : "W");
}

void GPS_waypoint::setColor(const QColor& c)
{
    if ( c != _color )
    {
        Modify();
        _color = c;
    }
}

/*
bool GPS_waypoint::operator==(const GPS_waypoint& other)
{
    return (latitude() == other.latitude()) &&
            (longitude() == other.longitude()) &&
            (altitude() == other.altitude()) &&
            (_name == other._name) &&
            (_description == other._description) &&
            (_color == other._color);
}*/

QString GPS_waypoint::serialize() const
{
    QColor clr = color();
    return QString("%1 %2 %3 ").arg( latitude(), 0, 'f' )
                               .arg( longitude(), 0, 'f')
                               .arg( altitude(), 0, 'f' ) +
           '"' + name().toAscii().toBase64() + '"' + ' ' +
           '"' + description().toAscii().toBase64() + '"' + ' ' +
           QString("%1 %2 %3").arg(clr.red())
                              .arg(clr.green() )
                              .arg( clr.blue( ));
}

bool GPS_waypoint::unserialize(QTextStream & stream, GPS_waypoint *wp)
{
    QByteArray tmp;
    int red,green,blue;

    double lat, lon, alt;
    stream >> lat;
    stream >> lon;
    stream >> alt;
    wp->setLatitude( lat );
    wp->setLongitude( lon );
    wp->setAltitude( alt );
    stream >> tmp;
    tmp.chop(1);
    tmp = tmp.mid(1);
    wp->setName( QByteArray::fromBase64(tmp ) );
    stream >> tmp;
    tmp.chop(1);
    tmp = tmp.mid(1);
    wp->setDescription( QByteArray::fromBase64(tmp) );
    stream >> red >> green >> blue;
    wp->setColor( QColor( red, green, blue ) );
    return true;
}
